package com.pactera.madp.cp.controller.sidebar.contact;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.dto.WorkContactTagUserCreateDTO;
import com.pactera.madp.cp.api.vo.WorkContactCrmTagVO;
import com.pactera.madp.cp.api.vo.WorkContactTagUserVO;
import com.pactera.madp.cp.api.vo.contact.ContactClientListVO;
import com.pactera.madp.cp.api.vo.contact.ContactMessageVxVO;
import com.pactera.madp.cp.api.vo.contact.ContactTrackVO;
import com.pactera.madp.cp.service.*;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * ContactWeChatController
 *
 * @author jun
 * @version 1.0
 * 2021/6/29 9:57
 **/
@RestController
@AllArgsConstructor
@RequestMapping("/workcontactwechat")
@Api(value = "workcontactwechat", tags = "客户管理-微信")
public class ContactWeChatController {

    private IContactService contactService;
    @Resource
    private IWorkContactTagService iWorkContactTagService;
    private IWorkContactTagPivotService iWorkContactTagPivotService;

    /**
     *
     * 查看客户详情基本信息
     *
     * @param wxExternalUserid
     * @param wxUserId
     * @return
     */
    @GetMapping("/show")
    @ApiOperation(value = "查看客户详情基本信息-微信")
    public R<ContactMessageVxVO> showContactInfoWx(String wxExternalUserid, String wxUserId) {
        ContactMessageVxVO vo = contactService.getContactInfoWx(wxExternalUserid,wxUserId);
        if (vo == null)
            return R.failed("客户或已删除您");
        return R.ok(vo);
    }

    @GetMapping("/show-del")
    @ApiOperation(value = "查询客户详情基本信息--已删除客户")
    public R<ContactMessageVxVO> showContactInfoWxDelete(String wxExternalUserid, String wxUserId){
        return R.ok(contactService.getContactInfoWxDelete(wxExternalUserid,wxUserId));
    }

    /**
     * 个人绑定企业标签
     *
     * @param dto
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "个人绑定企业标签")
    public R saveLabelByUser(@RequestBody WorkContactTagUserCreateDTO dto){
        iWorkContactTagService.saveLabelByUser(dto);
        return R.ok();
    }

    /**
     * 修改个人绑定企业标签
     *
     * @return
     */
    @SysLog("修改个人绑定企业标签")
    @DeleteMapping("/{wxExternalUserid}/{wxUserId}/{id}")
    @ApiOperation(value = "修改个人绑定企业标签")
    public R deleteLabelByUser(@PathVariable String wxExternalUserid, @PathVariable String wxUserId,@PathVariable Integer id){
        return iWorkContactTagPivotService.deleteLabel(wxExternalUserid, wxUserId,id)?R.ok():R.failed("删除失败");
    }

    /**
     * 查询个人绑定标签
     *
     * @param wxExternalUserid
     * @return
     */
    @GetMapping("/index-label")
    @ApiOperation(value = "查询个人绑定企业标签")
    public R<List<WorkContactTagUserVO>> LabelByUserList(@RequestParam(value = "wxExternalUserid") String wxExternalUserid, @RequestParam(value = "wxUserId")String wxUserId){
        return R.ok(iWorkContactTagService.selectLabelByUser(wxExternalUserid,wxUserId));
    }

    /**
     * 查询用户绑定的CRM系统标签
     *
     * @param wxExternalUserid
     * @return
     */
    @GetMapping("/crm-label")
    @ApiOperation(value = "查询个人绑定CRM系统标签")
    public R<List<WorkContactCrmTagVO>> getCrmTagListByWxUserId(@RequestParam(value = "wxExternalUserid") String wxExternalUserid, @RequestParam(value = "wxUserId")String wxUserId){
        return R.ok(iWorkContactTagService.getCrmTagListByWxUserId(wxExternalUserid,wxUserId));
    }

    @GetMapping("/track")
    @ApiOperation(value = "客户动态查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="wxExternalUserid",value="客户id",dataType="String"),
            @ApiImplicitParam(name="wxUserId",value="用户id",dataType="String"),
    })
    public R<Page<List<ContactTrackVO>>> contactTrack(String wxUserId,String wxExternalUserid,Page page) {
        if (wxExternalUserid == null) {
            return  R.failed("客户 id 不能为空");
        }
        return R.ok(contactService.getContactTrackByContactIdWechat(wxUserId,wxExternalUserid,page));
    }

    @GetMapping("/trajectory")
    @ApiOperation(value = "客户详情页动态查询")
    public  R trajectoryList(String wxUserId,String wxExternalUserid,Integer type,Page page){
        return contactService.getContactTrackListByContactIdWechat(wxUserId,wxExternalUserid,type,page);
    }


    @GetMapping("/client-list")
    @ApiOperation(value = "客户信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="wxUserId",value="用户id",dataType="String"),
            @ApiImplicitParam(name="roomId",value="客户群id",dataType="Integer")
    })
    public R<Page<ContactClientListVO>> getClientList(@RequestParam("wxUserId") String wxUserId,@RequestParam(name = "roomId",required = false) Integer roomId,Page page ){
        return contactService.getClientList(wxUserId,roomId,page);
    }

}
